package com.isunland.managesystem.utils;

import android.text.TextUtils;
import android.widget.TextView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.common.MyApplication;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/2/17 18:28
 */

public class MyStringUtil {
  public static final String COMMA = ",";
  @SuppressWarnings("unused") public static final String COLLON = "：";
  @SuppressWarnings("WeakerAccess") public static final String EMPTY_STR = "";
  public static final String SEPEREATE = "  |  ";
  //public static final String COLON="：";
  //public static final String HOLDER="%s";

  public static String connect(String... args) {
    if (args == null) return EMPTY_STR;
    StringBuilder sb = new StringBuilder();
    for (String temp : args) {
      if (temp == null) temp = "";
      sb.append(temp);
    }
    return sb.toString();
  }

  /**
   * 连接字符串,这样在string中不用声明重复
   */
  public static String connect(int... args) {
    if (args == null) return EMPTY_STR;
    StringBuilder sb = new StringBuilder();
    for (int temp : args) {
      sb.append(MyStringUtil.toString(MyApplication.getAppContext().getString(temp)));
    }
    return sb.toString();
  }

  public static String replaceAll(String original, String text, String withText) {
    if (original == null) return "";
    if (text == null) text = "";
    if (withText == null) withText = "";
    try {
      return original.replaceAll(text, withText);
    } catch (Exception e) {
      e.printStackTrace();
      return original;
    }
  }

  /**
   * 参数转postman
   */
  public static String paramsToPostman(Object params) {
    if (params == null) return EMPTY_STR;
    String paramsStr = params.toString();
    //03-02 13:47:06.966 21994-21994/com.isunland.managesystem.zhibaoyun I/LoginFragment.volleyPostLogin(Line:183): params={username=liuwp, password=1234567, mobileConfig=1, roleTypeFlag=}

    try {
      return paramsStr.substring(paramsStr.indexOf("{") + 1, paramsStr.indexOf("}"))
          .replaceAll(",", "\n")
          .replaceAll("=", ":");
    } catch (Exception e) {
      return "";
    }
  }

  /**
   * tosTring不为null
   */
  public static String toString(String str) {
    return str == null ? "" : str;
  }

  /**
   * 类型安全的转换函数.
   *
   * @param value 待转换
   * @param defaultValue 默认值
   * @return 数字.
   */
  public static double valueOfDouble(Object value, double defaultValue) {
    if (value == null || "".equals(value.toString().trim())) {
      return defaultValue;
    }
    try {
      return Double.valueOf(value.toString());
    } catch (NumberFormatException e) {
      return defaultValue;
    }
  }

  /**
   * 类型安全的转换函数.
   *
   * @param value 待转换
   * @param defaultValue 默认值
   * @return 数字.
   */
  public static int valueOfInt(Object value, int defaultValue) {
    if (value == null || "".equals(value.toString().trim())) {
      return defaultValue;
    }
    try {
      return Integer.valueOf(value.toString().trim());
    } catch (NumberFormatException e) {
      return defaultValue;
    }
  }

  public static String valueOf(Object value) {
    if (value == null || "".equals(value.toString().trim())) {
      return "";
    }
    return String.valueOf(value);
  }

  /**
   * 通过拼音搜索.
   *
   * @param originalStr 原字串,被搜索字串.
   * @param searchStr 搜索字串
   * @return 原字串的拼音是否包含搜索字串
   */
  public static boolean isContainsByFirstLetter(String originalStr, String searchStr) {
    try {
      searchStr = searchStr.trim().toLowerCase();
      originalStr = originalStr.trim().toLowerCase();
      String pinYinStr =
          PinyinParser.getInstance().setResource(originalStr).getSpelling().toLowerCase();
      String firstLetterStr = FirstLetterUtil.getFirstLetter(originalStr).toLowerCase();

      return originalStr.contains(searchStr)
          || pinYinStr.contains(searchStr)
          || firstLetterStr.contains(searchStr);
    } catch (Exception e) {
      return false;
    }
  }

  @SuppressWarnings("unused")
  public static boolean isContainsByPinYin(String originalStr, String searchStr) {
    try {
      searchStr = searchStr.trim().toLowerCase();
      originalStr = originalStr.trim().toLowerCase();
      String pinYinStr =
          PinyinParser.getInstance().setResource(originalStr).getSpelling().toLowerCase();
      return originalStr.contains(searchStr) || pinYinStr.contains(searchStr);
    } catch (Exception e) {
      return false;
    }
  }

  public static boolean isEmptyTextView(TextView tv) {
    return tv == null || TextUtils.isEmpty(tv.getText().toString().trim());
  }

  public static boolean isEmpty(String str) {
    return str == null || TextUtils.isEmpty(str.trim());
  }

  public static String getText(TextView tv) {
    if (tv == null) return "";
    return tv.getText().toString().trim();
  }

  /**
   * 将字符串逗号拆分为LIst<String>
   *
   * @param originalStr 原始字符
   * @param tagStr 拆分字符,如逗号,分号等
   * @return 拆分后的列表
   */
  public static ArrayList<String> splitStr(String originalStr, String tagStr) {
    if (TextUtils.isEmpty(originalStr) || TextUtils.isEmpty(tagStr)) {
      return null;
    }
    ArrayList<String> list = new ArrayList<>();
    // 保存最后一个逗号的位置
    int index = originalStr.indexOf(tagStr);
    //保存拆下来的字串.
    String item;
    while (index != -1) {
      // 第一个逗号之前是拆分后的字符串.
      item = originalStr.substring(0, index);
      list.add(item);
      //截取去掉拆掉的字串
      originalStr = originalStr.substring(index + 1);
      //更新拆分字符的位置
      index = originalStr.indexOf(tagStr);
    }
    //最后一个字串.
    list.add(originalStr);
    return list;
  }

  /**
   * 获取带默认值,已减少if的使用.
   * 如果原始字串为空就用默认值
   *
   * @param value 原始字串
   * @param defaultValue 默认值
   */
  public static String getStringByDefault(String value, String defaultValue) {

    if (value == null) value = "";
    if (defaultValue == null) defaultValue = "";

    if (TextUtils.isEmpty(value)) return defaultValue;
    return value;
  }

  /**
   * 字符串逗号拼接成String
   *
   * @param list 列表
   * @param tagStr 合并字符,如逗号,分号等
   * @return 合并后的字符串, 去掉最后一个连接字符
   */
  @SuppressWarnings("unused") public static String joinStr(List<String> list, String tagStr) {
    if (list == null || list.size() == 0) {
      return null;
    }
    if (tagStr == null) tagStr = "";
    StringBuilder sb = new StringBuilder();
    for (String str : list) {
      //如果获取的值为空,不用拼接,避免,,,,这样的情况.
      if (!MyStringUtil.isEmpty(str)) sb.append(str).append(tagStr);
    }
    //切掉最后一个字符.
    String result = sb.substring(0, sb.length() - 1);
    LogUtil.i(tagStr);
    return result;
  }

  /**
   * @param list 任意列表
   * @param field 成员字段
   * @param tagStr 连接字符
   * @param <T> 对象类型
   * @return 连接后字串
   */
  public static <T> String joinStr(ArrayList<T> list, String field, String tagStr) {
    if (list == null || list.size() == 0 || field == null || field.trim().length() == 0) {
      return null;
    }
    if (tagStr == null) tagStr = "";
    StringBuilder sb = new StringBuilder();
    String fieldValue;
    for (T item : list) {
      //如果获取的值为空,不用拼接,避免,,,,这样的情况.
      fieldValue = LocalSearch.getFieldValue(item, field);
      if (!MyStringUtil.isEmpty(fieldValue)) sb.append(fieldValue).append(tagStr);
    }
    //如果tag不为空,切掉最后一个字符.
    if (!MyStringUtil.isEmpty(tagStr)) {

      return MyStringUtil.substring(sb.toString(), 0, sb.length() - 1);
    } else {
      return sb.toString();
    }
  }

  /**
   * 安全的截取字串方法
   *
   * @param original 原字符串
   * @param start 开始位置
   * @param end 结束位置
   * @return 安全的子字串
   */
  @SuppressWarnings("unused") public static String substring(String original, int start, int end) {

    try {
      return original.substring(start, end);
    } catch (Exception e) {
      return EMPTY_STR;
    }
  }

  /**
   * 相等判断,排除异常
   */
  public static boolean equals(String str1, String str2) {
    return !(str1 == null || str2 == null) && str1.equals(str2);
  }

  /**
   * 相等判断,排除异常
   */
  public static boolean equalsIgnoreCase(String str1, String str2) {
    return !(str1 == null || str2 == null) && str1.equalsIgnoreCase(str2);
  }

  /**
   * 计算出类似于"1,2,3,4"这样的字符串解析成数字后的总值.
   *
   * @param originalStr 字符串
   * @param splitStr 分隔符,如逗号等.
   * @return double总值.
   */
  public static double getTotalDouble(String originalStr, String splitStr) {
    if (TextUtils.isEmpty(originalStr) || TextUtils.isEmpty(splitStr)) return 0;
    ArrayList<String> list = MyStringUtil.splitStr(originalStr, splitStr);
    if (list == null) return 0;
    double result = 0;
    for (String item : list) {
      result += MyStringUtil.valueOfDouble(item, 0);
    }
    return result;
  }

  public static String joinStr(HashMap<String, String> list, String tagStr) {
    if (list == null || list.size() == 0) {
      return null;
    }
    if (tagStr == null) tagStr = "";
    StringBuilder sb = new StringBuilder();
    for (Map.Entry<String, String> entry : list.entrySet()) {
      sb.append(entry.getKey()).append("=").append(entry.getValue()).append(tagStr);
    }
    //切掉最后一个字符.
    String result = sb.substring(0, sb.length() - 1);
    LogUtil.i(tagStr);
    return result;
  }

  public static boolean isComplete(String... strs) {
    if (strs == null) return false;
    for (String s : strs) {
      if (MyStringUtil.isEmpty(s)) {
        ToastUtil.show(R.string.notComplete);
        return false;
      }
    }
    return true;
  }

  public static String getAllUpperCase(String originalStr) {
    if (TextUtils.isEmpty(originalStr)) return "";
    String result = "";
    char c;
    for (int i = 0; i < originalStr.length(); i++) {
      c = originalStr.charAt(i);
      result += Character.isUpperCase(c) ? c + "" : "";
    }
    return result;
  }

  public static boolean contains(String original, String tag) {
    return original != null && (tag == null || original.contains(tag));
  }

  public static String toJson(Object obj) {
    try {
      return new Gson().toJson(obj);
    } catch (Exception e) {
      e.printStackTrace();
      return "";
    }
  }

  public static String formatNumber(Double number) {
    if (number == null || number == 0) return "0.00";
    try {
      return new java.text.DecimalFormat("#.00").format(number);
    } catch (Exception e) {
      e.printStackTrace();
      return "0.00";
    }
  }
}
